import omi from 'omi'
let { WeElement, define } = omi
import './index.css'

import '../../../components/time-picker'
import '../../../components/order-type-picker'

import api from '../../../api/api.js'
import { get, login,loading, hideLoading} from '../../../utils/util.js'
// import { LoginUrl } from '../../api/api.js'
const app = getApp();

define('index', class extends WeElement{
  config = {
    navigationBarTitleText: '庐山西海瑶池湾度假山庄'
  }

  data={
    activity_list: [],
    test: false
  }

  getUserInfo = (evt) => {
    console.log(evt)
    if (!wx.getStorageSync('userInfo')){
      app.globalData.userInfo = evt.detail.userInfo
      // this.data.userInfo = evt.detail.userInfo
      wx.setStorageSync('userInfo', evt.detail.userInfo);
      // this.data.hasUserInfo = true
      login(evt.detail, app, () => {
        console.log(evt.currentTarget.dataset.index);
        let activity_index = evt.currentTarget.dataset.index;
        wx.navigateTo({
          url: '../scenicDetail/index?activity_id=' + this.data.activity_list[activity_index]['activity_id']
        })
      });
    }else{
      let activity_index = evt.currentTarget.dataset.index;
      wx.navigateTo({
        url: '../scenicDetail/index?activity_id=' + this.data.activity_list[activity_index]['activity_id']
      })
    }
    
    this.update()
  }  

  viewDetail=(e)=>{
    if (app.globalData.userInfo){
      
    }else{
      // wx.showToast({
      //   title: '未登录',
      // })
    }
    
  }

  toDraw=()=>{
    wx.showToast({
      title: '本次活动已结束，敬请期待后期活动',
      icon: "none"
    })
  }

  install(){
    // this.update();
    // console.log(omi.h)
    loading('加载中...');
    get(api.get_activity_list).then((res)=>{
      console.log(res);
      this.data.activity_list = res.data;
      this.update();
      wx.hideLoading();
    })
  }

  onAbc = (evt) => {
      console.log("evt", evt.detail.date);
    this.data.test = evt.detail.date
    this.update()
    }


  render(){
    return(
      <view scroll-y="true" class="discover_index">
        <text class="draw_title">积分抽奖</text>
        <view class="draw_wrap">
          <text>瑶池湾幸运大抽奖</text>
          <button bindtap={toDraw}>立即抽奖</button>
        </view>
        <text class="show_title">展示</text>
        <view class="show_list">
          {
            activity_list.map((activity,index)=>(
              <button open-type="getUserInfo" bindgetuserinfo="getUserInfo" class="scenic_wrap" data-index={index} bindtap={this.viewDetail} key={index}>
                <view class="scenic_pic">
                  <image src={activity.full_diagram}/>
                </view>
                <text class="scenic_txt line_ellipsis">{activity.activity_name}</text>
              </button>
            ))
          }
        </view>
      </view>
    )
  }
})